package com.duguo.dynamicmvc.support.servlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.duguo.dynamicmvc.controller.RootController;
import com.duguo.dynamicmvc.support.ModelAndView;
import com.duguo.dynamicmvc.support.pathinfo.PathInfo;
import com.duguo.dynamicmvc.support.pathinfo.PathInfoParser;
import com.duguo.dynamicmvc.view.forward.ForwardView;
import com.duguo.dynamicmvc.view.render.ViewRender;

public class RequestHandlerImpl implements RequestHandler {
    private RootController _rootController;
    private PathInfoParser pathInfoParser;
	private ViewRender viewRender;
	
	public RequestHandlerImpl(){
		//TODO: convert this to none static acess
		ForwardView.setRequestHandler(this);
	}

    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        PathInfo pathInfo = pathInfoParser.parsePathInfo(request);
        ModelAndView modelAndView = _rootController.handleRequest(request, response, pathInfo);
        viewRender.render(request, response,modelAndView);
    }

    public void setRootController(RootController rootController) {
        _rootController = rootController;
    }

    public void setViewRender(ViewRender viewRender) {
        this.viewRender = viewRender;
    }

    public void setPathInfoParser(PathInfoParser pathInfoParser) {
        this.pathInfoParser = pathInfoParser;
    }
}
